package com.Action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import transformFactory.TransformDate;

import com.Entity.Orders;
import com.Entity.Userdata;
import com.Entity.Userhealthdata;
import com.Entity.Users;
import com.Service.OrderService;
import com.Service.UserHealthDataService;
import com.Service.UserdataService;

public class CommunityAction {
	
	private static final Logger logger = Logger.getLogger(CommunityAction.class);

	private String result=null;
	private Userhealthdata pregnantData;
	@Autowired
	private UserHealthDataService pregnantDataService;
	private List<Userhealthdata> pregnantdatalist=null;
	private List<Userhealthdata> datalistbean=null;
	private Orders orders;
	private Orders orderbean;
	@Autowired
	private OrderService orderservice; 
	private List<Orders> orderslist=null;
	
	
	HttpSession session = ServletActionContext.getRequest().getSession();
	
	public String displayPregnantData() throws Exception {
		int doctorid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		logger.info("doctorid:"+doctorid);
		pregnantdatalist=this.pregnantDataService.findByDoctorId(doctorid);
		result="displayPregnantDataSucc";
		return result;
	}
	
	public String writeOrder() throws Exception {
		int dataId=Integer.parseInt(ServletActionContext.getRequest().getParameter("dataId"));
		logger.info("dataId:"+dataId);
		pregnantData=this.pregnantDataService.getById(dataId);
		session.setAttribute("pregnantData", pregnantData);
		return "writeOrder";
	}
	
	public String addOrder() throws Exception {
		pregnantData=(Userhealthdata) session.getAttribute("pregnantData");
		int doctorid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		int usertype=Integer.parseInt(ServletActionContext.getRequest().getParameter("usertype"));
		int dataid=pregnantData.getDataId();
		int motherid=pregnantData.getUserId();
		
		orderslist=this.orderservice.findByDataId(dataid);
		if(orderslist==null||orderslist.isEmpty()){		
			logger.info("还没有写医嘱！");
			orderbean.setUserId(doctorid);
			orderbean.setUserType((short) usertype);
			orderbean.setDataId(dataid);
			orderbean.setMotherId(motherid);
			orderbean.setOrderUploadTime(new Date());
			this.orderservice.save(orderbean);
			pregnantData=this.pregnantDataService.getById(dataid);
			pregnantData.setOrderState((short) 1);
			this.pregnantDataService.update(pregnantData);
			pregnantdatalist=this.pregnantDataService.findByDoctorId(doctorid);
			return "addOrderSucc";
		}
		else{
			logger.info("已经写过医嘱了！");
			orders=orderslist.get(0);
			orders.setOrderUploadTime(new Date());
			orders.setOrderContent(orderbean.getOrderContent());
			this.orderservice.update(orders);
			pregnantdatalist=this.pregnantDataService.findByDoctorId(doctorid);
			return "addOrderSucc";
		}	
	}
	
	public String modifyOrder() throws Exception {
		int dataId=Integer.parseInt(ServletActionContext.getRequest().getParameter("dataId"));
		logger.info("dataId:"+dataId);
		pregnantData=this.pregnantDataService.getById(dataId);
		logger.info("pregnantdata:"+pregnantData.getBloodSugar());
		orderslist=this.orderservice.findByDataId(dataId);
		orders=orderslist.get(0);
		session.setAttribute("pregnantData", pregnantData);
		return "modifyOrder";
	}
	
	public String listOrder() throws Exception {
		int doctorid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		pregnantdatalist=this.pregnantDataService.findOrders(doctorid, (short) 1);
		return "listOrderSucc";
	}
	
	public String displayOrder() throws Exception {
		int dataId=Integer.parseInt(ServletActionContext.getRequest().getParameter("dataId"));
		pregnantData=this.pregnantDataService.getById(dataId);
		orderslist=this.orderservice.findByDataId(dataId);
		orders=orderslist.get(0);
		return "displayOrderSucc";
	}
	
	public String transferHealthData() throws Exception {
		int dataId=Integer.parseInt(ServletActionContext.getRequest().getParameter("dataId"));
		int doctorid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		pregnantData=this.pregnantDataService.getById(dataId);
		pregnantData.setTransferState((short) 1);
		this.pregnantDataService.update(pregnantData);
		pregnantdatalist=this.pregnantDataService.findByDoctorId(doctorid);
		return "transferHealthDataSucc";
	}
	
	public String cancelTransfer() throws Exception {
		int dataId=Integer.parseInt(ServletActionContext.getRequest().getParameter("dataId"));
		int doctorid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		pregnantData=this.pregnantDataService.getById(dataId);
		pregnantData.setTransferState((short) 0);
		this.pregnantDataService.update(pregnantData);
		pregnantdatalist=this.pregnantDataService.findByDoctorId(doctorid);
		return "cancelTransferSucc";
	}	
	
	public List<Userhealthdata> getPregnantdatalist() {
		return pregnantdatalist;
	}

	public void setPregnantdatalist(List<Userhealthdata> pregnantdatalist) {
		this.pregnantdatalist = pregnantdatalist;
	}
	
	public Userhealthdata getPregnantData() {
		return pregnantData;
	}

	public void setPregnantData(Userhealthdata pregnantData) {
		this.pregnantData = pregnantData;
	}
	
	public Orders getOrders() {
		return orders;
	}

	public void setOrders(Orders orders) {
		this.orders = orders;
	}

	public Orders getOrderbean() {
		return orderbean;
	}

	public void setOrderbean(Orders orderbean) {
		this.orderbean = orderbean;
	}

}
